

public class StudentTest {
    static void main() {
        Student[] arr = new Student[3];
        Student stu1 = new Student(1, "陈平安", 18);
        Student stu2 = new Student(2, "魏晋", 20);

        arr[0] = stu1;
        arr[1] = stu2;


        Student stu4 = new Student(3, "阮秀", 17);
        boolean flag = contains(arr, stu4.getId());
        if (flag) {
            System.out.println("当前ID重复，请更换ID");
        } else {
            int count = getCount(arr);
            if (count == arr.length) {
                Student[] newArr = createNewArr(arr);
                newArr[count] = stu4;
            } else {
                arr[count] = stu4;
            }
        }
    }

        static void printArr (Student[]arr){
            for (int i = 0, i<arr.length, i++){
                Student stu = arr[i];
                if (stu != null) {
                    System.out.println(stu.getId() + "," + stu.getName() + "," + stu.getAge());
                }
            }
        }

    




    static Student[] createNewArr(Student[] arr) {
        Student[] newArr = new Student[arr.length + 1];
        for(int i=0;i<arr.length;i++) {
            newArr[i]=arr[i];}
        return newArr;
    }






    static int getCount(Student[] arr) {int count=0;
        for(int i=0;i<arr.length;i++) {if(arr[i] != null) {count++;}}
    return count;
    }





    static boolean contains(Student[] arr, int id) {
        for(int i=0;i<arr.length;i++) {
            Student stu = arr[i];
            int sid = stu.getId();
            if(sid==id) {
                return true;
            }
        }return false;
    }
}
